import { getKeyRouter } from '@/utils';
import { history } from 'umi';

const parsePurchaseString = (inputString) => {
  let PurchaseTableName = [
    'purchase_need',
    'purchase_apply',
    'purchase_source_intent',
    'purchase_source_change',
    'purchase_source_clarify',
    'purchase_source_calibrate',
    'purchase_source_expert',
    'purchase_source', //这个需要放最后，不然有source在中间的就会先用这个
  ];
  // 直接将接收的字符串作为id
  let id = inputString;
  // 提取字符串部分，截取到purchase_source
  let key = PurchaseTableName.find((item) => inputString.startsWith(item));
  // 如果没有找到匹配项，返回false
  if (!key) {
    return false;
  }
  // 返回结果
  return {
    id: id,
    key: key,
  };
};

/**
 *
 * @param payload 参数列表
 * @param homePageRouterText 参数列表不存在时跳转当前页，location.pathname
 *
 */
export const jumpRoutes = (payload: Record<string, any> | any, homePageRouterText: any) => {
  if (payload && payload?.id) {
    if (payload?.businessID && parsePurchaseString(payload?.businessID)) {
      let resObj = parsePurchaseString(payload?.businessID);
      if (resObj) {
        let { id, key } = resObj;
        if (key == 'purchase_need') {
          //采购需求OA页面
          history.push({
            pathname: `/purchaseNeedController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_apply') {
          //采购申请OA页面
          history.push({
            pathname: `/purchaseApplyController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source_intent') {
          //采购意向公开OA页面
          history.push({
            pathname: `/purchaseSourceIntentController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source_change') {
          //采购方式变更OA页面
          history.push({
            pathname: `/purchaseSourceChangeController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source') {
          //招标文件编辑OA页面
          history.push({
            pathname: `/purchaseSourceController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source_clarify') {
          //澄清修改OA页面
          history.push({
            pathname: `/purchaseSourceClarifyController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source_calibrate') {
          //定标OA页面
          history.push({
            pathname: `/purchaseSourceCalibrateController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'purchase_source_expert') {
          //评标代表OA页面
          history.push({
            pathname: `/purchaseSourceExpertController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        }
      }
    }
  } else {
    history.push(homePageRouterText); //去管理界面
  }
};
